package com.example.healthcondition.services.impl;

import com.example.healthcondition.entity.User;
import com.example.healthcondition.mapper.UserMapper;
import com.example.healthcondition.services.UserServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Optional;

@Service
public class UserServerImpl implements UserServer {

    @Autowired
    private UserMapper userMapper;

    @Override
    public boolean validateUser(String username, String password) {
        Optional<User> userOpt = userMapper.findByUsername(username);
        if (userOpt.isPresent()) {
            User user = userOpt.get();
            // 直接比较明文密码
            return user.getPassword().equals(password);
        }
        return false;  // 用户不存在
    }

    @Override
    public Integer getUserIdByUsername(String username) {
        Optional<User> userOpt = userMapper.findByUsername(username);
        return userOpt.map(User::getUserId).orElse(null); // 返回 userId，如果用户不存在则返回 null
    }

}

